package cn.dxs.gitee.http.core;

import android.support.annotation.Nullable;

import retrofit2.Response;

/**
 * http响应
 *
 * @author lijian
 * @date 2019-09-21 16:14
 */
public class HttpResponse<T extends Object> {

    private Response<T> oriResponse;

    public HttpResponse(Response<T> response) {
        oriResponse = response;
    }

    /**
     * 成功
     *
     * @return
     */
    public boolean isSuccessful() {
        return oriResponse.isSuccessful();
    }

    /**
     * 来自缓存
     *
     * @return
     */
    public boolean isFromCache() {
        return isResponseEnable(oriResponse.raw().cacheResponse());
    }

    /**
     * 来自网络
     *
     * @return
     */
    public boolean isFromNetWork() {
        return isResponseEnable(oriResponse.raw().networkResponse());
    }

    /**
     * 响应包是可用的
     *
     * @param response
     * @return
     */
    private boolean isResponseEnable(@Nullable okhttp3.Response response) {
        return response != null && response.code() == 200;
    }

    /**
     * 获取原始响应包
     *
     * @return
     */
    public Response<T> getOriResponse() {
        return oriResponse;
    }

    /**
     * 获取响应体
     *
     * @return
     */
    public T body() {
        return oriResponse.body();
    }
}
